/**
 * Created by yzb2b on 15-1-21.
 */
var express = require('express');
var router = express.Router();
var logger = require('../../log/logConfig').logger;//引入日志
var global = require('../global');

var http = require('http');
var url = require('url');
var util = require('util');
/**
 * 拦截器
 * 添加于所有路由之前判断用户是否登录
 */


function fnLogin_Filter(req, res,next) {
    var aUrl = global.REQ_URL.split(',');
    var temp=0;
    var reqUrl=req.url;
    var reqUrlC=reqUrl.indexOf('?');
    if(reqUrlC>0){
        reqUrl=reqUrl.substring(0,reqUrlC);
    }
    for(var i = 0;i < aUrl.length ; i++){
        if(aUrl[i] == reqUrl){
            temp += 1;
            break;
        }
    }
    if(temp == 0){//登录前不放行的
        if(!req.session){
            logger.error('redis未启动');
            //如果redis未启动，也跳到登录页
            res.render('login');
            return;
        }else if(!req.session.sessionName){
           // res.render('login');
            res.redirect('login');//跳转到首页
            return;
        }
    }

    next();
}

exports.LOGIN_FILTER = fnLogin_Filter;

